package com.china08.yxyapi.api.manage.tec.teaching.zujuan.manualpaper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.service.manage.tec.teaching.zujuan.ZujuanQuestionService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.ApiOperation;

/**
 * 手动组卷---按章节目录选题
 * 
 */
@RestController
@CrossOrigin
public class ZujuanQuesListByChapterController extends YxyApiBaseController {

    @Autowired
    private ZujuanQuestionService zujuanQuestionService;

    @Autowired
    private YxyQuesKnowRepository quesKnowRepository;

    @ApiOperation(value = "组卷系统手动组卷，老师按章节选题")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/chapter/queslist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        String userId = getProfile(request, response).getId();
        // 排序
        Pageable pageable = null;
        if (StringUtils.equals(reqModel.sortField, "0")) {
            pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        }
        if (StringUtils.equals(reqModel.sortField, "1")) {
            pageable = reqModel.getPageable(Direction.DESC, "homeworkFavoriteCount");
        }
        if (StringUtils.equals(reqModel.sortField, "2")) {
            pageable = reqModel.getPageable(Direction.DESC, "homeworkUseCount");
        }

        Page<ElasticYxyYxyQuesResource> page = zujuanQuestionService.questionFindByBookcontentAndLevelAndTypeAndUse(
                reqModel.bookcontent, reqModel.difficultyLevel, reqModel.questionType, reqModel.srcType, userId,
                reqModel.bookId, pageable);
        List<RespModel> list = new ArrayList<RespModel>();
        RespModel model = null;
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : page) {
            model = new RespModel();
            model.id = elasticYxyYxyQuesResource.getId();
            model.difficultyLevel = elasticYxyYxyQuesResource.getDifficultyLevel();
            model.questionType = elasticYxyYxyQuesResource.getQuestionType();
            model.quesBody = elasticYxyYxyQuesResource.getQuesBody();
            model.favorite = zujuanQuestionService.checkCollectionQuestion(userId, model.id);
            // model.quesTimes =
            // zujuanQuestionService.countQuestionUse(model.id);
            model.quesTimes = (elasticYxyYxyQuesResource.getHomeworkUseCount() == null ? 0
                    : elasticYxyYxyQuesResource.getHomeworkUseCount());
            // 取交集知识点
            List<Integer> knowIdList = zujuanQuestionService.getBookContentKnowList(reqModel.bookcontent,
                    elasticYxyYxyQuesResource.getKnowledgeIds());
            List<YxyQuesKnow> knows = quesKnowRepository.findByIdIn(knowIdList);
            List<String> knowNameList = knows.stream().map(x -> x.getName()).collect(Collectors.toList());
            model.knowIds = knowIdList;
            model.knowledgeTexts = knowNameList;
            list.add(model);
        }
        Page<RespModel> resPage = new PageImpl<ZujuanQuesListByChapterController.RespModel>(list, pageable,
                page.getTotalElements());
        return resPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 书的id
        public String bookId;
        // 目录id
        public String bookcontent;
        // 难度级别
        public String difficultyLevel;
        // 题型
        public String questionType;
        // 题的来源类型
        public String srcType;
        // 排序字段 ： 0 时间 1 收藏次数 2 使用次数
        public String sortField;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(bookcontent, null), "请选择章节目录");
        }
    }

    protected static class RespModel {
        // 题id
        public String id;
        // 题的难度级别
        public String difficultyLevel;
        // 题所属的知识点集合
        public List<Integer> knowIds;
        // 题的知识点名称
        public List<String> knowledgeTexts;
        // 题型
        public String questionType;
        // 题干
        public Object quesBody;
        // 是否收藏
        public boolean favorite;
        // 题的使用次数
        public Long quesTimes;
    }
}
